#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2023-06-26 23:11
# @Author : hard work
# @File    : snmpV1_V2_do_set_util.py
# @Description :
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902
cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorindex, varBinds = cmdGen.setCmd(
    cmdgen.CommunityData('private'),#写入Community
    cmdgen.UdpTransportTarget(('172.16.10.2',161)),#IP地址和端口号
    ('1.3.6.1.2.1.1.5.0',rfc1902.OctetString('SNMPv2R2'))#OID和写入的内容，需要进行编码！
)

if errorIndication:
    print(errorIndication)
elif errorStatus:
    print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorindex and varBinds[int(errorindex)-1][0] or '?'
        )
    )
for name,val in varBinds:
    print('%s = %s' % (name.prettyPrint(),val.prettyPrint()))#打印修改的结果